"""
API Routes for Unified Notification Service
"""
from fastapi import APIRouter, Depends, HTTPException, status, Body
from sqlalchemy.ext.asyncio import AsyncSession
from pydantic import BaseModel, EmailStr

from ..services.notification_service import NotificationService
from ..core.exceptions import NotificationError
from ..main import get_notification_service, get_db_session # Will be created

router = APIRouter(prefix="/notifications", tags=["Notifications"])

# --- Request Models ---

class SendEmailRequest(BaseModel):
    user_id: str
    recipient: EmailStr
    subject: str
    content: str

class SendEmailResponse(BaseModel):
    message: str
    notification_id: str

# --- API Endpoints ---

@router.post(
    "/email/send",
    response_model=SendEmailResponse,
    status_code=status.HTTP_202_ACCEPTED
)
async def send_email(
    request: SendEmailRequest,
    service: NotificationService = Depends(get_notification_service),
    session: AsyncSession = Depends(get_db_session)
):
    """
    Accepts a request to send an email.

    The service will attempt to dispatch the email and log the result.
    This endpoint returns immediately with a notification ID.
    """
    try:
        notification_id = await service.send_email(
            db_session=session,
            user_id=request.user_id,
            recipient=request.recipient,
            subject=request.subject,
            content=request.content
        )
        return SendEmailResponse(
            message="Email dispatch accepted.",
            notification_id=notification_id
        )
    except NotificationError as e:
        # Service-level errors (e.g., email not configured, DB logging failed)
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail=str(e)
        )
    except Exception as e:
        # Catch any other unexpected errors
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"An unexpected error occurred: {str(e)}"
        )

# ... Endpoints for SMS, Push, etc. would be added here ...